#include <vector>
#include <sstream>
#include <iomanip>
#include <stdlib.h>
#include "date.h"
#include "split.h"

#include <time.h>
#include <stdio.h>

using namespace std;
using namespace stock_trade;

Date::Date(const string& strDate)
{
    vector<string> vecDate;
    split(strDate, "-", vecDate);
    if (vecDate.size() != 3)
        return;
    year = atoi(vecDate[0].c_str());
    month = atoi(vecDate[1].c_str());
    day = atoi(vecDate[2].c_str());
}

Date Date::GetNextDay()
{
    return Date(0,0,0);
}

string Date::toString() const
{
    ostringstream oss;
    oss << (*this);
    return oss.str();
}

bool Date::operator==(const Date& date) const
{
    return year == date.year && month == date.month && day == date.day;
}

bool Date::operator!=(const Date& date) const
{
    return !(*this == date);
}

bool Date::operator>(const Date& date) const
{
    if (year > date.year)
        return true;
    if (year < date.year)
        return false;

    if (month > date.month)
        return true;
    if (month < date.month)
        return false;
    
    if (day > date.day)
        return true;
    if (day < date.day)
        return false;
    return false;
}

bool Date::operator<(const Date& date) const
{
    if (!(*this == date) && !(*this > date))
        return true;
    return false;
}

bool Date::operator>=(const Date& date) const
{
    return (*this > date) || (*this == date);
}

bool Date::operator<=(const Date& date) const
{
    return (*this < date) || (*this == date);
}

int Date::operator-(const Date& date) const
{
    return day - date.day 
        + (month - date.month) * 30
        + (year - date.year) * 365;
}

Date Date::operator++()
{
    if (day != 31)
    {
        day++;
        return *this;
    }
    //new month
    day = 1;
    if (month != 12)
    {
        month++;
        return *this;
    }
    //new year
    month = 1;
    year++;
    return *this;

}

Date Date::operator--()
{
    if (day > 0)
    {
        day--;
        return *this;
    }
    //new month
    day = 31;
    if (month > 0)
    {
        month--;
        return *this;
    }
    //new year
    month = 12;
    year--;
    return *this;

}

ostream& operator<<(ostream& os, const Date& date)
{
    os << date.year << "-" << setfill('0') << setw(2) << date.month << "-" << setfill('0') << setw(2) << date.day;
    return os;
}

Date GetToday()
{
    time_t tm;
    tm = time(NULL);
    struct tm *tblock;
    tblock = localtime(&tm);
    Date date(1900+tblock->tm_year, tblock->tm_mon + 1, tblock->tm_mday);
    return date;
}

Date GetYesterday()
{
    Date date = GetToday();
    return --date;
}
